﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.MainForm;
using GMS.MainClasses;
using GMS.ServiceFunctions.FunctionForControls;
using GMS.ServiceFunctions.Data;
using System.Data.SqlClient;
using System.Reflection;
using GMS.Enums;

namespace GMS.ViewUsers
{
    public partial class ViewUsers : ChildForms
    {
        private FunctionsForDataGridView FFDGV = new FunctionsForDataGridView();
        BindingSource mbs = new BindingSource();
        public ViewUsers()
        {
            InitializeComponent();
            StoredProcedures = new String[2];
            StoredProcedures[0] = "SelectDatabaseUser";
            StoredProcedures[1] = "DeactivateDatabaseUser";
        }
        void RefreshGrid()
        {
            mbs.DataSource = MRF.ReturnDataSetDB(ConnectionString, StoredProcedures[0]);
            mbs.DataMember = "table";
        }
        private void ViewUsers_Load(object sender, EventArgs e)
        {
            RefreshGrid();
            dataGridView1.DataSource = mbs;
        }
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            FFDGV.TextForGroupBox(groupBox1, dataGridView1, "Список пользователей        ");
        }

        private void AddUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddEditUser AEU = new AddEditUser(EditMode.Editing);
            AEU.ConnectionString = ConnectionString;
            AEU.MdiParent = MRF;
            AEU.Show();
            
        }
        private void EditUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddEditUser AEU = new AddEditUser(EditMode.Editing);
            AEU.ConnectionString = ConnectionString;
            AEU.IdUser = Convert.ToInt32(dataGridView1.CurrentRow.Cells["IdUser"].Value);
            AEU.MdiParent = MRF;
            AEU.Show();
        }
        private void DeleteUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы хотите удалить пользователя!!! Вы уверены?", "Предупреждение!!!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                AddEditUser AEU = new AddEditUser(EditMode.Deleting);
                AEU.ConnectionString = ConnectionString;
                AEU.IdUser = Convert.ToInt32(dataGridView1.CurrentRow.Cells["IdUser"].Value);
                AEU.MdiParent = MRF;
                AEU.Show();
            }
        }

        private void DeactivateUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MRF.ExecuteSqlCommand(StoredProcedures[1] + " " + dataGridView1.CurrentRow.Cells["IdUser"].Value.ToString(), ConnectionString);
            RefreshGrid();
        }
        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {
            mbs.Filter = "Логин like '%" + textBox4.Text.Trim() + "%'";
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            mbs.Filter = "[Имя] like '%" + textBox1.Text.Trim() + "%'";
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            mbs.Filter = "[Фамилия] like '%" + textBox2.Text.Trim() + "%'";
        }

        private void textBox3_KeyUp(object sender, KeyEventArgs e)
        {
            mbs.Filter = "[Отчество] like '%" + textBox3.Text.Trim() + "%'";
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (e.RowIndex == dataGridView1.RowCount - 1)
            { }
            else
            FFDGV.PaintGridRows(dataGridView1, e, dataGridView1.CurrentRow.Cells["UserStatus"].ColumnIndex, -1, System.Drawing.Color.Red);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            AddUserToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            EditUserToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            DeactivateUserToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            DeleteUserToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            AboutUserForm AUF = new AboutUserForm();
            AUF.Show();
        }
        private void ViewUsers_Activated(object sender, EventArgs e)
        {
            RefreshGrid();
        }
    }
}
